/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web.dic;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.dic.Storefront;
import com.boxin.ims.modules.wechat.service.dic.StorefrontService;

/**
 * 实体店Controller
 * @author Jakemanse
 * @version 2013-12-10
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/dic/storefront")
public class StorefrontController extends BaseController {

	@Autowired
	private StorefrontService storefrontService;
	
	@ModelAttribute
	public Storefront get(@RequestParam(required=false) Long id) {
		if (id != null){
			return storefrontService.get(id);
		}else{
			return new Storefront();
		}
	}
	
	@RequiresPermissions("wechat:dic:storefront:view")
	@RequestMapping(value = {"list", ""})
	public String list(Storefront storefront, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			storefront.setUser(user);
		}
        Page<Storefront> page = storefrontService.find(new Page<Storefront>(request, response), storefront); 
        model.addAttribute("page", page);
		return "modules/wechat/dic/storefrontList";
	}

	@RequiresPermissions("wechat:dic:storefront:view")
	@RequestMapping(value = "form")
	public String form(Storefront storefront, Model model) {
	
		User user = UserUtils.getUser();
		if (storefront.getUser() == null || storefront.getUser().getId() <1){
			storefront.setUser(user);
		}
		
		model.addAttribute("storefront", storefront);
		return "modules/wechat/dic/storefrontForm";
	}

	@RequiresPermissions("wechat:dic:storefront:edit")
	@RequestMapping(value = "save")
	public String save(Storefront storefront, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, storefront)){
			return form(storefront, model);
		}
		storefrontService.save(storefront);
		addMessage(redirectAttributes, "保存实体店'" + storefront.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/dic/storefront/?repage";
	}
	
	@RequiresPermissions("wechat:dic:storefront:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		storefrontService.delete(id);
		addMessage(redirectAttributes, "删除实体店成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/dic/storefront/?repage";
	}

}
